package org.bluweb.g4wicket.analytics;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.html.IHeaderResponse;
import org.bluweb.g4wicket.G4WicketBehaviour;
import org.bluweb.g4wicket.G4WicketEvent;


@SuppressWarnings("serial")
public class AnalyticsTracker extends G4WicketBehaviour {

	private String account;
	
	public AnalyticsTracker( String account ) {
		this.account = account;
	}

	@Override
	public void renderHead( IHeaderResponse response ) {

		super.renderHead( response );
		
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "account", account );

		response.renderJavascript( this.getJavaScript( "gaq.js", parameters ), "queue" );
		
		response.renderJavascript( this.getJavaScript( "AnalyticsTracker.js", null ), "tracker" );
	}
	
	public void trackEvent( final String logEvent, G4WicketEvent event ) {

		this.execute( "_gaq.push(['_trackEvent', '" + event.getComponent().getId() + "', '" + logEvent + "'])", event );
	}
}
